<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>广告自动关闭</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .father {
            position: relative;
            width: 800px;
            height: 500px;
            margin: 50px auto;
        }

        .father img {
            display: block;
            width: 100%;
            height: 100%;
        }

        .son {
            display: flex;
            width: 200px;
            height: 30px;
            position: absolute;
            right: 20px;
            top: 20px;
            background-color: #ccc;
            justify-content: space-around;
            align-items: center;
        }

        .right {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="father">
        <img src="../images/desktop_9.jpg" alt="">
        <div class="son">
            <div class="left">广告在 <span>10</span> 秒后消失</div>
            <div class="right">关闭</div>
        </div>
    </div>

    <script>
        // 获取标签元素
        const span = document.querySelector('span');
        const cl = document.querySelector('.father .right');

        // 声明初始值
        let time = 10;

        // 开启一个定时器
        setInterval(() => {
            time--;
            span.innerHTML = time;
            if (time <= -1) {
                cl.parentNode.parentNode.style.display = 'none';
            };
        }, 1000);

        // 给关闭盒子创建点击事件
        cl.addEventListener('click', function (e) {
            cl.parentNode.parentNode.style.display = 'none';
        });

    </script>
</body>

</html>